package com.tanxi521.com.thenorthtitle.util;

import com.tanxi521.com.thenorthtitle.entity.PlayerTitle;
import com.tanxi521.com.thenorthtitle.entity.TheNorthTitleData;
import com.tanxi521.com.thenorthtitle.sql.dao.TnTitleBaseInfoMapper;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.command.CommandSender;

import java.util.ArrayList;
import java.util.List;

public class PageUtil {

    public static void topPage(int currentPage, CommandSender sender) {
        int count = TnTitleBaseInfoMapper.getUserTitlesCount(sender.getName());
        int perPage = 10;
        int totalPage = count % perPage == 0 ? count / perPage : count / perPage + 1;
        List<PlayerTitle> userTitles = TnTitleBaseInfoMapper.getUserTitles(currentPage == 1 ? 0 : (currentPage - 1) * perPage, perPage, sender.getName());
        TheNorthTitleData nowTitle = TnTitleBaseInfoMapper.getUserTitle(sender.getName());
        String[] blank = new String[50];
        for (String s : blank) s = " ";
        sender.sendMessage(blank);
        sender.sendMessage("§a========================[ §6" + sender.getName() + " 的称号 §a]========================");
        sender.sendMessage("§b§l当前佩戴称号: " + nowTitle.getTitleName());
        sender.sendMessage("§e§l点击下方称号替换称号");

        int i = currentPage == 1 ? currentPage : (currentPage - 1) * perPage + 1;
        for (PlayerTitle userTitle : userTitles) {
            ArrayList<BaseComponent> bc = new ArrayList<>();
            TextComponent tc1 = new TextComponent("§e" + i + ". §r" + userTitle.getTitleName() + "                      ");
            ClickEvent ce = new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/tntitle set " + userTitle.getId());
            HoverEvent he = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[]{
                    new TextComponent("§e称号名称: " + userTitle.getTitleName() + "\n"),
                    new TextComponent("§e称号价值: " + userTitle.getPrice() + " 银币" + '\n'),
                    new TextComponent("§e银币商店优惠: " + (int) (Double.parseDouble(userTitle.getTitleDisCount()) * 100) + '%' + '\n'),
                    new TextComponent("§e前置称号: " + userTitle.getTitlePre() + '\n'),
                    new TextComponent("§e获得时间: " + userTitle.getAchieveTime().substring(0, userTitle.getAchieveTime().length() - 2))
            });
            tc1.setClickEvent(ce);
            tc1.setHoverEvent(he);
            bc.add(tc1);
            BaseComponent[] bcR = new BaseComponent[bc.size()];
            bc.toArray(bcR);
            sender.spigot().sendMessage(bcR);
            i++;
        }
        sender.sendMessage("");
        ArrayList<BaseComponent> bc = new ArrayList<>();
        TextComponent tc1 = new TextComponent("§2点击上一页");
        ClickEvent ce1 = new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/tntitle me " + (currentPage <= 1 ? 1 : currentPage - 1));
        HoverEvent he1 = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[]{new TextComponent("§e点击查看第" + (currentPage <= 1 ? 1 : currentPage - 1) + "页")});
        tc1.setClickEvent(ce1);
        tc1.setHoverEvent(he1);
        bc.add(tc1);
        TextComponent tc3 = new TextComponent("§a  " + currentPage + "/" + totalPage + " 共 " + count + " 个称号  ");
        bc.add(tc3);
        TextComponent tc2 = new TextComponent("§2点击下一页");
        ClickEvent ce2 = new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/tntitle me " + (currentPage >= totalPage ? totalPage : currentPage + 1));
        HoverEvent he2 = new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[]{new TextComponent("§e点击查看第" + (currentPage >= totalPage ? totalPage : currentPage + 1) + "页")});
        tc2.setClickEvent(ce2);
        tc2.setHoverEvent(he2);
        bc.add(tc2);
        BaseComponent[] bcR = new BaseComponent[bc.size()];
        bc.toArray(bcR);
        sender.spigot().sendMessage(bcR);
    }
}
